package cn.kais.drawer.bean;

import cn.kais.drawer.UnitUtil;
import cn.kais.drawer.coms.TextCom;
import cn.kais.drawer.menu.MenuCodeBarFormat;
import cn.kais.drawer.unit.UnitCode;
import com.alibaba.fastjson.JSONObject;

import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.text.AttributedString;

/**
 * 条形码
 */
public class BeanCodeBAR extends BeanBasic {

    public String code = "123456";

    public int top;
    public int left;
    public int width;
    public int height;

    public int codeHeight; // 条码高度
    public int textHeight; // 文本高度

    public Color bColor; // 背景颜色
    public Color mColor; // 主色
    public Color fontColor; // 文本颜色

    public int textPosition; // 文本显示在条码的方位  0 不显示 1顶部 2底部

    public JSONObject textStyle; // 文本样式

    public MenuCodeBarFormat format; // 码类型

    public Graphics2D sd; // 绘制

    public BeanCodeBAR(Graphics2D sd, JSONObject com) {
        this.sd = sd;
        this.code = com.getString("text");

        this.top = com.getInteger("top");
        this.left = com.getInteger("left");
        this.width = com.getInteger("width");
        this.height = com.getInteger("height");

        this.codeHeight = com.getIntValue("codeHeight");

        this.bColor = UnitUtil.toColor(com.getString("bgColor"));
        this.mColor = UnitUtil.toColor(com.getString("fontColor"));

        this.textPosition = com.getIntValue("textPosition");

        this.textStyle = com.getJSONObject("textStyle");
        this.textStyle.put("text", this.code);
        this.textHeight = this.textStyle.getIntValue("lineHeight");

        this.format = MenuCodeBarFormat.onParseCode(com.getString("encodingType"));

        this.create();
    }

    private void create() {
        // 区域框 测试使用
        if (this.isShowBorder) {
            new BeanBorder(this.sd, this.top, this.left, this.width, this.height);
        }
        sd.setColor(this.bColor);
        sd.fillRect(this.left, this.top, this.width, this.height);
        System.out.println("合并宽度：" + this.width);
        System.out.println("合并高度：" + this.height);

        BufferedImage ima = UnitCode.iCodeBuffer(this.format, this.code, this.width, this.codeHeight, this.mColor, this.bColor);
        System.out.println("输出码宽度：" + ima.getWidth());
        System.out.println("输出码高度：" + ima.getHeight());

        int iy = 0;
        if (this.textPosition == 1) { // 如果显示在顶部,需要调整 码 y轴位置
            iy = this.textHeight;
        }
        sd.drawImage(ima, this.left, iy + this.top, this.width, this.codeHeight, null);

        if (this.textPosition != 0) {
            TextCom com = onCreateText(this.textStyle);
            if (null == com) return;
            System.out.println("文本宽度：" + com.width);
            System.out.println("文本高度：" + com.height);
            int ty = (this.textHeight - com.height) / 2;
            if (this.textPosition == 2) { // 如果文本显示在底部,需要调整文本y轴位置
                System.out.println("文本显示在底部");
                ty = ty + this.codeHeight;
            }
            int x = (this.width - com.width) / 2;
            sd.setColor(this.mColor);
            sd.drawString(com.attr.getIterator(), x + this.left, ty + this.top + com.height);
        }

    }

    /**
     * 每个组件Json创建独立的 text组件
     *
     * @param com 组件Json
     */
    private TextCom onCreateText(JSONObject com) {
        String text = com.getString("text");
        if (text.equals("-")) return null;

        String fontName = com.getString("font");
        int fontSize = com.getIntValue("fontSize");
        // 样式：加粗 斜体 下划线
        boolean isBold = com.getBooleanValue("isBold");  // 是否加粗
        boolean isItalic = com.getBooleanValue("isItaly"); // 是否斜体
        String isUnderline = com.getString("isUnderline"); // 是否下划线
        String isCenterline = com.getString("isCenterline"); // 是否中横线

        // 字体
        Font font = UnitUtil.toFont(fontName, fontSize, isBold, isItalic);
        FontMetrics metrics = this.sd.getFontMetrics(font);
        int fontHeight = metrics.getAscent() - metrics.getDescent() - metrics.getLeading();
        int fontWidth = metrics.stringWidth(text);

        // 绘制配置
        AttributedString as = new AttributedString(text);
        as.addAttribute(TextAttribute.FONT, font);
        as.addAttribute(TextAttribute.FOREGROUND, this.fontColor);
        if (null != isUnderline && !isUnderline.equals("none")) { // 下划线
            as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        }
        if (null != isCenterline && !isCenterline.equals("none")) { // 中横线
            as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
        }
        // 对齐方式
        int align = com.getIntValue("alignType");
        return new TextCom(as, fontWidth, fontHeight, align);
    }

}
